import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../commponts/login.vue'
import Home from '../commponts/home.vue'
import Welcome from '../commponts/welcome.vue'
import Users from '../commponts/users.vue'
import Roles from '../commponts/power/roles.vue'
import Rights from '../commponts/power/rights.vue'
import Report from '../commponts/report.vue'

Vue.use(VueRouter)

const routes = [
	{ path: '/', redirect: '/login' },
	{ path: '/login', component: Login },
	{ 
		path: '/home', 
		component: Home,
		redirect:'/Welcome',
		children:[
			{ path: '/welcome',component:Welcome},
			{ path: '/users',component:Users},
			{ path: '/roles',component:Roles},
			{ path: '/rights',component:Rights},
			{ path: '/reports',component:Report}
		]
	},
]

const router = new VueRouter({
  routes
})


//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
	//to 将要访问的路径
	//from 代表从哪个路劲跳转而来
	//next 是一个函数，表示放行
		// next()放行      next('/login')强制跳转到login界面
	if (to.path === '/login') return next()
	//如果路由地址不是登录界面，则 获取token
	const tokenStr = window.sessionStorage.getItem('token')
	// 判断token是否在，没有token则自动跳转到登录界面
	if (!tokenStr) return next('/login')
	next()
})


export default router
